package com.shop.cloud.upms.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.cloud.common.core.constant.CommonConstants;
import com.shop.cloud.upms.admin.mapper.ShopUserMapper;
import com.shop.cloud.upms.admin.service.*;
import com.shop.cloud.upms.admin.service.ShopUserService;
import com.shop.cloud.upms.admin.service.SysUserRoleService;
import com.shop.cloud.upms.common.entity.SysUser;
import com.shop.cloud.upms.common.entity.SysUserRole;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author
 */
@Slf4j
@Service
@AllArgsConstructor
public class ShopUserServiceImpl extends ServiceImpl<ShopUserMapper, SysUser> implements ShopUserService {
	private static final PasswordEncoder ENCODER = new BCryptPasswordEncoder();
	private final SysUserRoleService sysUserRoleService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean save(SysUser sysUser, String roleId) {
		sysUser.setDelFlag(CommonConstants.STATUS_NORMAL);
		sysUser.setPassword(ENCODER.encode(sysUser.getPassword()));
		baseMapper.insert(sysUser);
		//新增用户角色关联
		SysUserRole userRole = new SysUserRole();
		userRole.setUserId(sysUser.getId());
		userRole.setRoleId(roleId);
		sysUserRoleService.save(userRole);
		return false;
	}
}
